//
//  XiaoFeiSQController.m
//  PosApp
//
//  Created by 刘萍 on 2018/8/31.
//  Copyright © 2018年 李英新. All rights reserved.
//

#import "XiaoFeiSQController.h"
#import "XiaoFeiSQHead01.h"
#import "XiaoFeiSQCell01.h"
#import "XiaoFeiSQCell02.h"
#import "PosModel.h"
#import "BannerModel.h"

#import "PosNewsController.h"

@interface XiaoFeiSQController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,XiaoFeiSQCell01Delegate,XiaoFeiSQCell02Delegate>

@property (weak, nonatomic) IBOutlet UIView *XFBackView;
@property (weak, nonatomic) IBOutlet UICollectionView *XFCollectView;
@property (strong, nonatomic) XiaoFeiSQHead01 * HeadV;
@property (strong, nonatomic) NSMutableArray * DataArr;
@property (strong, nonatomic) NSMutableArray * BannerArr;
@property (strong, nonatomic) MBProgressHUD * hud;

@end

@implementation XiaoFeiSQController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor]}];
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = @"消费神器";
    UIButton * leftBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
    [leftBtn setImage:[UIImage imageNamed:@"MainBackImage_W"] forState:UIControlStateNormal];
    leftBtn.imageEdgeInsets = UIEdgeInsetsMake(5,0,5,20);
    [leftBtn addTarget:self action:@selector(ClickLeftBarButtonItem) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * leftItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    CAGradientLayer *gl = [CAGradientLayer layer];
    gl.frame = CGRectMake(0,0,KScreenW,KScreenH/2);
    gl.startPoint = CGPointMake(0, 0);
    gl.endPoint = CGPointMake(1, 1);
    gl.colors = @[(__bridge id)[UIColor colorWithRed:255/255.0 green:109/255.0 blue:1/255.0 alpha:1].CGColor,(__bridge id)[UIColor colorWithRed:251/255.0 green:41/255.0 blue:67/255.0 alpha:1].CGColor];
    gl.locations = @[@(0.0),@(1.0f)];
    [self.XFBackView.layer addSublayer:gl];
    
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    
    self.XFCollectView.collectionViewLayout = layout;
    self.XFCollectView.delegate = self;
    self.XFCollectView.dataSource = self;
    
    [self.XFCollectView registerNib:[UINib nibWithNibName:@"XiaoFeiSQHead01" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head"];
    [self.XFCollectView registerNib:[UINib nibWithNibName:@"XiaoFeiSQCell01" bundle:nil] forCellWithReuseIdentifier:@"cell01"];
    [self.XFCollectView registerNib:[UINib nibWithNibName:@"XiaoFeiSQCell02" bundle:nil] forCellWithReuseIdentifier:@"cell02"];
    [self UpLoadDataOfXiaoFeiSQController];
    [self UpLoadDataOfBannerView];
}

- (void)UpLoadDataOfBannerView{
    NSMutableDictionary * dic = [[NSMutableDictionary alloc] init];
    [dic setObject:@"2" forKey:@"type"];
    [dic setObject:KUseriPhone forKey:@"phone"];
    
    [MainObjectWay PostWithMethod:@"plug" andAction:@"Server" andAddParam:YES parameters:dic success:^(id responseObject) {
        NSLog(@"返回的信息 = %@",responseObject);
        if ([MainObjectWay IsSucceedWithCode:responseObject[@"code"]]) {
            NSMutableArray * arr = responseObject[@"data"][@"list"];
            for (int i = 0 ; i < arr.count; i ++) {
                NSMutableDictionary * dic = arr[i];
                BannerModel * model = [BannerModel mj_objectWithKeyValues:dic];
                [self.BannerArr addObject:model];
                
            }
            [self.HeadV AddBannerImage:self.BannerArr];
        }
        
    } failure:^(NSError *error) {
        NSLog(@"错误信息 = %@",error);
    }];
}

- (void)UpLoadDataOfXiaoFeiSQController{
    self.hud = [MainObjectWay CreateHUDView:self.view];
    NSMutableDictionary * dic = [[NSMutableDictionary alloc] init];
    [dic setObject:KUseriPhone forKey:@"phone"];
    [MainObjectWay PostWithMethod:@"list" andAction:@"Pos" andAddParam:YES parameters:dic success:^(id responseObject) {
        NSLog(@"Pos请求的数据 = %@",responseObject);
        
        if ([MainObjectWay IsSucceedWithCode:responseObject[@"code"]]) {
            NSMutableArray * arr = responseObject[@"data"][@"list"];
            for (int i = 0; i < arr.count; i ++) {
                NSMutableDictionary * dic = arr[i];
                PosModel * model = [PosModel mj_objectWithKeyValues:dic];
                [self.DataArr addObject:model];
            }
        }
        [self.XFCollectView reloadData];
        [self.hud hideAnimated:YES];
    } failure:^(NSError *error) {
        NSLog(@"错误信息 = %@",error);
        [self.hud hideAnimated:YES];
    }];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 2;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (self.DataArr.count > 0) {
        if (section == 0) {
            return 1;
        }else{
            return self.DataArr.count-1;
        }
    }else{
        return 0;
    }
    
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    if (section == 0) {
        return UIEdgeInsetsMake(0, 0, 0, 0);
    } else{
        return UIEdgeInsetsMake(10, 10, 10, 10);
    }
    
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return CGSizeMake(KScreenW, 160);
    }else{
        CGFloat W = (KScreenW - 20)/2;
        return CGSizeMake(W, 280);
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        XiaoFeiSQCell01 * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell01" forIndexPath:indexPath];
        cell.tag = indexPath.row;
        cell.delegate = self;
        PosModel * model = self.DataArr[0];
        [cell setModel:model];
        return cell;
    }else{
        XiaoFeiSQCell02 * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell02" forIndexPath:indexPath];
        cell.tag = indexPath.row+1;
        cell.delegate = self;
        [cell CreateXiaoFeiSQCell02];
        
        PosModel * model = self.DataArr[indexPath.row+1];
        [cell setModel:model];
        return cell;
    }
    
}

- (void)ClickOnBtnFromXiaoFeiSQCell01ToLookNewsWithCellID:(NSUInteger)cellID{
    PosModel * model = self.DataArr[0];
    PosNewsController * PosC = [[PosNewsController alloc] init];
    PosC.PosID = model.pos_id;
    [self.navigationController pushViewController:PosC animated:YES];
}

- (void)ClickOnBtnFromXiaoFeiSQCell02ToLookNewsWithCellID:(NSUInteger)cellID{
    PosModel * model = self.DataArr[cellID];
    PosNewsController * PosC = [[PosNewsController alloc] init];
    PosC.PosID = model.pos_id;
    [self.navigationController pushViewController:PosC animated:YES];
}



//header的size
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return CGSizeMake(KScreenW, 180);
    }else{
        return CGSizeMake(KScreenW, 0);
    }
}

//通过设置SupplementaryViewOfKind 来设置头部或者底部的view，其中 ReuseIdentifier 的值必须和 注册是填写的一致，本例都为 “reusableView”
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    self.HeadV = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head" forIndexPath:indexPath];
    [self.HeadV CreateOneControllerHead01];
    
    return self.HeadV;
}


- (void)ClickLeftBarButtonItem{
    [self.navigationController popViewControllerAnimated:YES];
}


- (NSMutableArray *)DataArr {
    if (!_DataArr) {
        _DataArr = [NSMutableArray array];
    }
    return  _DataArr;
}

- (NSMutableArray *)BannerArr {
    if (!_BannerArr) {
        _BannerArr = [NSMutableArray array];
    }
    return _BannerArr;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/



@end
